// __PUBLIC__/js/page/event/admin/eventtypelist
require(['../../common'], function() {
  require(['admin.eventtypelist'])
})

define('admin.eventtypelist', [
  'jquery',
  'page/grid',
  'css!ke/themes/default/default.css',
  'ke/kindeditor',
], function($, Grid) {
  'use strict';

  var $table = $('.table')
  var mygrid = new Grid($table)
  mygrid.init();

  function del(selected) {
    $.post(APP + '?app=event&m=Admin&a=deleteType', {
      id: selected.id.join()
    }).done(function(text) {
      if (text == 1) {
        mygrid.del(selected.index)
      } else if (text == 2) {
        mygrid.del(selected.index)
      } else if (text == 0) {
        alert('lang.transfer_type');
      } else {
        alert('lang.del_err');
      }
    })
  }

  function getHtml(name) {
    var html = '<div class="form"><dl class="kv"><dt><label>类型：</label></dt>'
    html += '<dd><input class="input-text" id="J_typename" class="txt" value="' + name + '" />'
    html += '</dd></dl></div></div>'

    return html
  }



  KindEditor('#J_addtype').click(function() {
    var html = getHtml('')

    var dialog = KindEditor.dialog({
      width: 500,
      height: 150,
      title: '增加分类',
      body: html,
      closeBtn: {
        name: '关闭',
        click: function(e) {
          dialog.remove();
        }
      },
      yesBtn: {
        name: '确定',
        click: function(e) {
          var typename = $('#J_typename').val()
          if (typename.replace(/\s+/g, "").length == 0) {
            alert('类型名称不能为空')
            return false
          }

          if (typename.length > 19) {
            alert('类型名称不能超过19个字')
            return false
          }

          $.post(APP + '?app=event&m=Admin&a=addType ', {
            name: typename
          }).done(function(id) {
            if (id === '-2') {
              alert('类型名称不能为空')
              return false
            } else if (id === '0') {
              alert('类型名称已存在')
              return false
            } else {
              if (id == '1') {
                alert('添加成功')
                dialog.remove()
                setTimeout(function() {
                  window.location.reload(true);
                }, 1000);
              } else {
                alert('添加失败')
              }
            }
          })
        }
      },
      noBtn: {
        name: '取消',
        click: function(e) {
          dialog.remove();
        }
      }
    });
  })


  KindEditor('.A_edittype').click(function() {
    var typeid = $(this).closest('tr').data('id')
    var name = $(this).data('name')

    var html = getHtml(name)

    var dialog = KindEditor.dialog({
      width: 500,
      height: 150,
      title: '编辑分类',
      body: html,
      closeBtn: {
        name: '关闭',
        click: function(e) {
          dialog.remove();
        }
      },
      yesBtn: {
        name: '确定',
        click: function(e) {
          var typename = $('#J_typename').val()
          if (typename.replace(/\s+/g, "").length == 0) {
            alert('类型名称不能为空')
            return false
          }

          if (typename.length > 19) {
            alert('类型名称不能超过19个字')
            return false
          }

          $.post(APP + '?app=event&m=Admin&a=updateType ', {
            name: typename,
            id: typeid
          }).done(function(id) {
            if (id === '-2') {
              alert('类型名称不能为空')
              return false
            } else if (id === '0') {
              alert('类型名称已存在')
              return false
            } else {
              if (id == '1') {
                alert('修改成功')
                dialog.remove()

                var url = APP + '?app=event&m=Admin&a=eventList&type=' + typeid
                $('#name_' + typeid).html('<a href="'+url+'">' + typename + '</a>');
              } else {
                alert('修改失败')
              }
            }
          })
        }
      },
      noBtn: {
        name: '取消',
        click: function(e) {
          dialog.remove();
        }
      }
    });
  })

  $(document)
    .on('click', '#J_del', function(e) {
      e.preventDefault()
      var selected = mygrid.getSelected()
      if (!selected.count) {
        alert('请选择一条数据')
      } else {
        if (confirm('确定删除已选择的内容？')) {
          del(selected)
        }
      }
    })
    .on('click', '.A_del', function(e) {
      e.preventDefault()
      if (confirm('确定删除已选择的内容？')) {
        var current = mygrid.getCurrent(this)
        var selected = {
          id: [current.id],
          index: [current.index]
        }
        del(selected)
      }
    })

})